package org.stimuliband.microphone;

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import org.apache.log4j.Logger;

/**
 *
 */
public class CaptureIntoWav implements MicrophoneListener {

    private static Logger logger = Logger.getLogger(Demo.class.getSimpleName());

    public CaptureIntoWav(File file) throws UnsupportedAudioFileException, IOException {
        AudioSystem.write(null, AudioFileFormat.Type.WAVE, file);
    }

    @Override
    public void onCaptured(byte[] captured) {
    }

    public static void main(String[] args) throws LineUnavailableException, IOException {

        int samplingRate = 8000;
        int groupSize = 205;
        int amplitudeThreshold = 50;

        MicrophoneObserverable microphone = new MicrophoneObserverable(samplingRate, groupSize, new HighVolumeListener(amplitudeThreshold));

        logger.info("Listening microphone...");
        microphone.start();
        Thread recording = new Thread(microphone);
        recording.start();

        System.in.read();

        logger.info("Stopping listening microphone...");
        microphone.stop();
        logger.info("Application stopped.");
    }
}
